package com.bookhouse.controller;

import com.bookhouse.entity.Book;
import com.bookhouse.entity.Manager;
import com.bookhouse.entity.Menu;
import com.bookhouse.entity.Reader;
import com.bookhouse.service.BookService;
import com.bookhouse.service.ManagerService;
import com.bookhouse.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by CiusCheung on 16/4/21.
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Resource(name = "managerService")
    private ManagerService managerService;
    @Resource(name = "menuService")
    private MenuService menuService;
    @Resource(name = "bookService")
    private BookService bookService;

    @RequestMapping(value = "login")
    public String checkLogin(Manager manager, HttpServletRequest request) {
        String hql = "From Manager m Where m.name ='" + manager.getName() + "' and m.password = '" + manager.getPassword() + "'";
        List<Manager> managers = managerService.find(hql);
        if (manager != null && managers.size()>0) {
            manager = managers.get(0);
            request.setAttribute("manager", manager);
            request.setAttribute("menuList", menuService.getAllMenu());
            request.setAttribute("bookCount", bookService.findCount(Book.class));
            System.out.print(bookService.findCount(Book.class));
            return "layout";
        }
        else {
            request.setAttribute("errors", "账号或密码输入有误!");
            return "login";
        }
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request){
        request.removeAttribute("manager");
        return "login";
    }

}
